package com.itqf.config;

import com.itqf.quarz.JobFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import java.util.Properties;


@Configuration
public class QuartzConfig {
    @Resource
    JobFactory jobFactory;



//        @Bean
//        public SchedulerFactoryBean schedulerFactoryBean() {
//            SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
//            schedulerFactoryBean.setJobFactory(jobFactory);
//            // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
//            schedulerFactoryBean.setOverwriteExistingJobs(true);
//            //延长启动
//            schedulerFactoryBean.setStartupDelay(1);
//            //设置加载的配置文件
//            schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));
//            return schedulerFactoryBean;
//        }
//
//        @Bean
//        public Scheduler scheduler() {
//            return schedulerFactoryBean().getScheduler();
//        }
//

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
//        @Qualifier(value = "druidDatasource") DataSource dataSource

        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        Properties p = new Properties();
        p.setProperty("org.quartz.scheduler.instanceName","MyQuartzScheduler");
        p.setProperty("org.quartz.scheduler.instanceid","AUTO");
        p.setProperty("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
        p.setProperty("org.quartz.threadPool.threadCount","10");
        p.setProperty("org.quartz.jobStore.class","org.quartz.impl.jdbcjobstore.JobStoreTX");
        p.setProperty(" org.quartz.jobStore.tablePrefix","QRTZ_");
        schedulerFactoryBean.setQuartzProperties(p);
        schedulerFactoryBean.setJobFactory(jobFactory);
        schedulerFactoryBean.setAutoStartup(true);

        schedulerFactoryBean.setOverwriteExistingJobs(true);//覆盖已知job
        schedulerFactoryBean.setStartupDelay(5);//

//        schedulerFactoryBean.setDataSource(dataSource);

        return  schedulerFactoryBean;

    }



}
